package com.rabbitmq.product;

import com.rabbitmq.constant.RabbitConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("sms")
@Api(value = "RabbitMQ生产者")
public class SmsProduct {

    @Resource
    private AmqpTemplate rabbitTemplate;

    /**
     * sms直连生产者
     *
     * @param sms
     * @return
     */
    @GetMapping("sendDirectData")
    @ApiOperation(value = "sms直连生产者", notes = "sms直连生产者")
    @ApiImplicitParam(value = "消息", name = "sms直连生产者", defaultValue = "sms直连生产者测试")
    public String sendSmsData(@RequestParam("sms") String sms) {
        rabbitTemplate.convertAndSend("huawei_direct_sms_exchange", "sms_direct_key", sms, message -> {
            // 设置消息过期时间 10秒过期
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        return "成功";
    }

    /**
     * 扇形交换机生产者
     *
     * @param sms
     * @return
     */
    @ApiOperation(value = "扇形生产者", notes = "扇形生产者")
    @ApiImplicitParam(value = "消息", name = "扇形生产者", defaultValue = "扇形生产者测试")
    @GetMapping("sendFanoutData")
    public String sendFanoutData(@RequestParam("sms") String sms) {
        rabbitTemplate.convertAndSend("huawei_fanout_exchange", "", sms);
        return "成功";
    }

    /**
     * 主题交换机生产者
     *
     * 路由建：
     *      sms.topic.key
     *      email.topic.key
     *      sms.topic.big.key
     *      email.topic.big.key
     *
     * @param sms
     * @return
     */
    @ApiOperation(value = "主题生产者", notes = "主题生产者")
    @ApiImplicitParam(value = "消息", name = "主题生产者", defaultValue = "主题生产者测试")
    @GetMapping("sendTopicData")
    public String sendTopicData(@RequestParam(value = "sms", defaultValue = "主题生产者测试") String sms) {
        // 测试主题的sms模糊匹配
//        rabbitTemplate.convertAndSend("huawei_topic_exchange", "sms.topic.key", sms);
//        rabbitTemplate.convertAndSend("huawei_topic_exchange", "sms.topic", sms);

        // 测试主题的email模糊匹配
//        rabbitTemplate.convertAndSend("huawei_topic_exchange", "email.topic.key", sms);
        rabbitTemplate.convertAndSend("huawei_topic_exchange", "email.topic", sms);

        return "成功";
    }
}
